package com.victor.controller.web;

import java.util.List;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletOutputStream;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.victor.entity.Copyright;
import com.victor.service.CopyrightService;
import com.victor.utils.Msg;

/**
 * 版权方管理
 * 
 * @author zhangbin
 *
 */
@RestController
@RequestMapping("/copyright")
public class CopyrightController {

	@Autowired
	private CopyrightService copyrightService;

	// 获取所有版权方列表
	@RequestMapping("getAllCopyright")
	public Msg getAllCopyright() {
		List<Copyright> copyrightlist = copyrightService.getAllCopyright();
		return Msg.success().add("copyrightlist", copyrightlist);
	}

	// 新建版权方
	@RequestMapping("createCopyright")
	public Msg createCopyright(Copyright copyright) {
		// 通过新建的账号去数据库查询是否重复
		Copyright copyrightData = copyrightService.loginexistCopyright(copyright.getCr_account());
		if (copyrightData != null) {
			return Msg.fail("已存在该版权方账号");
		}
		// 插入数据库版权方
		boolean insResult = copyrightService.createCopyright(copyright);
		if (insResult) {
			return Msg.success();
		}
		return Msg.fail();
	}

	// 修改信息
	@RequestMapping("updateCopyright")
	public Msg updateCopyright(Copyright copyright) {
		if (copyright.getId() == 0) {
			return Msg.fail("参数错误");
		}
		boolean updResult = copyrightService.updateCopyright(copyright);
		if (updResult) {
			return Msg.success();
		}
		return Msg.fail();
	}

	// 批量删除版权方
	@RequestMapping("batchDeleteCopyright")
	public Msg batchDeleteCopyright(int[] ids) {
		boolean delResult = copyrightService.batchDeleteCopyright(ids);
		if (delResult) {
			return Msg.success();
		}
		return Msg.fail();

	}

	// 导出版权方列表(全部)
	@RequestMapping("exportCopyright")
	public void exportCopyright(HttpServletResponse response, HttpServletRequest request) throws Exception {
		// 数据库查询数据 exportlist 需要导出的list
		List<Copyright> copyrightlist = copyrightService.getAllCopyright();

		// 创建poi导出数据对象
		SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook();
		// 创建sheet页
		Sheet sheet = sxssfWorkbook.createSheet("版权方列表");

		CellRangeAddress region1 = new CellRangeAddress(0, 1, (short) 0, (short) 6);
		// 参数1：起始行 参数2：终止行 参数3：起始列 参数4：终止列
		sheet.addMergedRegion(region1);
		Row headTitle = sheet.createRow(0);
		headTitle.createCell(0).setCellValue("版权方列表");

		// 创建表头
		Row headRow = sheet.createRow(4);
		// 设置表头信息
		headRow.createCell(0).setCellValue("序号");
		headRow.createCell(1).setCellValue("编号");
		headRow.createCell(2).setCellValue("版权方名称");
		headRow.createCell(3).setCellValue("版权方登录账号");
		headRow.createCell(4).setCellValue("版权方登录密码");
		headRow.createCell(5).setCellValue("备注");
		headRow.createCell(6).setCellValue("创建时间");

		// 遍历上面数据库查到的数据
		for (Copyright pm : copyrightlist) {
			Row dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
			dataRow.createCell(0).setCellValue(pm.getId());
			dataRow.createCell(1).setCellValue(pm.getIdentifier());
			dataRow.createCell(2).setCellValue(pm.getCopyrightname());
			dataRow.createCell(3).setCellValue(pm.getCr_account());
			dataRow.createCell(4).setCellValue(pm.getCr_password());
			dataRow.createCell(5).setCellValue(pm.getRemarks());
			dataRow.createCell(6).setCellValue(pm.getCreatetime());

		}
		// 下载导出文件名
		String filename = "版权方列表";
		// 设置头信息
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/vnd.ms-excel");
		// 一定要设置成xlsx格式
		response.setHeader("Content-Disposition",
				"attachment;filename=" + URLEncoder.encode(filename + ".xlsx", "UTF-8"));
		// 创建一个输出流
		ServletOutputStream outputStream = response.getOutputStream();
		// 写入数据
		sxssfWorkbook.write(outputStream);
		// 关闭
		outputStream.close();
		sxssfWorkbook.close();
	}

	// 版权方登录
	@RequestMapping("/copyrightLogin")
	public Msg copyrightLogin(Copyright copyright) {
		if (copyright.getCr_account().equals("")) {
			return Msg.fail("用户名不能为空");
		}
		if (copyright.getCr_password().equals("")) {
			return Msg.fail("密码不能为空");
		}

		// 通过版权方账号判断是否存在该版权方
		Copyright copyrightData = copyrightService.loginexistCopyright(copyright.getCr_account());
		if (copyrightData != null) {// 判断版权方是否存在
			Copyright copyright1 = copyrightService.loginCopyright(copyright);// 版权方登录判断
			if (copyright1 != null) {
				return Msg.success().add("copyright", copyright1);
			}
			return Msg.fail("密码错误");
		}
		return Msg.fail("版权方账户不存在");
	}

	@RequestMapping("/search")
	public Msg search(String name) {
		List<Copyright> search = copyrightService.search(name);
		return Msg.success().add("copyrightlist", search);

	}

}
